package com.sghd.fight.module.game.service.configure;

import com.sghd.fight.module.game.config.RobotConfig;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.Magic;
import com.sghd.fight.module.game.core.model.BattleSkill;
import com.sghd.fight.module.game.service.GameService;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.room.PvpRoom;
import com.sghd.fight.module.game.service.room.Robot;
import com.sghd.fight.module.game.service.work.RoomTimer;
import com.sghd.pojo.fight.game.PlayerVo;
import com.sghd.pojo.fight.game.RoomType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author frank
 * @date 2018-10-25 10:10
 * @desc
 **/
@Component
public class PvpConfigure extends ConfigureTemplate {
    @Autowired
    private GameService gameService;

    @Override
    public RoomType getType() {
        return RoomType.PVP;
    }

    @Override
    public PvpRoom build(long roomId, RoomTimer timer) {
        return new PvpRoom(roomId, getType(), gameService, configService);
    }

    @Override
    public Player transformRobot(PlayerVo robotVo, IRoom room, Date date) {
        //机器人配置
        List<RobotConfig> robotConfigs = configService.getRobotConfigs();
        Collections.shuffle(robotConfigs);
        RobotConfig robotConfig = robotConfigs.get(0);
        Map<Integer, Integer> skill2Level = robotConfig.getSkill2Level();
        Player robot = new Robot(robotVo.getPlayerId(), configService.getRobotName(), robotVo.getIndex(), room,
                new HashMap<>(skill2Level), new HashMap<>(robotConfig.getElement2Level()), robotConfig);
        Map<Integer, BattleSkill> battleSkill = robot.getSkills();
        for (Integer skillId : skill2Level.keySet()) {
            int level = skill2Level.get(skillId) == null ? 0 : skill2Level.get(skillId);
            SkillConfig skillConfig = configService.getSkillConfig(skillId);
            battleSkill.put(skillId, new BattleSkill(skillId, skillConfig.getCool(robot.getSkillLevel(skillId)), level));
        }
        Magic magic = new Magic();
        magic.setMagics(new HashMap<>(robotConfig.getInitMagic()));
        robot.setMagic(magic);
        return robot;
    }
}
